﻿<%@ WebHandler Language="C#" Class="personal" %>

using System;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using common;
using common.web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing.Imaging;

public class personal : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.AddHeader("Access-Control-Allow-Origin", "*");
        string _t = context.Request["t"];
        context.Response.Write(getData(_t));
    }

    string getData(string t)
    {
        switch (t)
        {
            case "MyDownload":
                return MyDownload();                    //我的下载
            case "MyApply":
                return MyApply();                       //我的申请
            case "MyMessage":
                return MyMessage();                     //我的消息
            case "MyDownloadByPage":
                return MyDownloadByPage();              //我的更多下载
            case "MyApplyByPage":
                return MyApplyByPage();                 //我的更多申请
            case "MyMessageByPage":
                return MyMessageByPage();               //我的更多消息       
            case "Login":
                return Login();                         //登录 
            case "LoginOut":
                return LoginOut();                      //退出登录 
            case "EditPass":
                return EditPass();                      //修改密码         
        }
        return "缺少参数!";
    }

    /// <summary>
    /// 我的下载
    /// </summary>
    protected string MyDownload()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string sql = "select a.id,a.fuid,a.cUser,fDptid,cFilename,fFileclid,cFilesize,fFiletype,a.tDate,a.cPic,cCk,cDescribe,fIschecked,fIsonsale,fDownloads,fApplyforupdate,fApplyforsale,cOffsale,tShsj,fShjg,cShms from [t_Filecollection] a with(nolock) join [t_Filesrecording] b with(nolock) on a.id = b.fFileid where b.cType = 1 and a.fuid = " + _uid;
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 我的申请
    /// </summary>
    protected string MyApply()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string sql = "select a.id,a.fuid,a.cUser,fDptid,cFilename,fFileclid,cFilesize,fFiletype,a.tDate,a.cPic,cCk,cDescribe,fIschecked,fIsonsale,fDownloads,fApplyforupdate,fApplyforsale,cOffsale,tShsj,fShjg,cShms from [t_Filecollection] a with(nolock) join [t_Filesrecording] b with(nolock) on a.id = b.fFileid where b.cType = 2 and a.fuid = " + _uid;
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 我的消息
    /// </summary>
    protected string MyMessage()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string sql = "select id,cTitle,cContent,cDptids,tDate from [t_Announcement] a with(nolock) ";
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cTitle"].ToString();
                inv.Date = dr["cContent"].ToString();
                inv.Pic = dr["cDptids"].ToString();
                inv.Describe = dr["tDate"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 我的更多下载
    /// </summary>
    protected string MyDownloadByPage()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "a.id,a.fuid,a.cUser,fDptid,cFilename,fFileclid,cFilesize,fFiletype,a.tDate,a.cPic,cCk,cDescribe,fIschecked,fIsonsale,fDownloads,fApplyforupdate,fApplyforsale,cOffsale,tShsj,fShjg,cShms","[t_Filecollection] a with(nolock) join [t_Filesrecording] b with(nolock) on a.id = b.fFileid where b.cType = 1 and a.fuid = " + _uid ," order by a.id desc ", out rsct, out pgct);
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("Rsct", rsct);
        jo.Add("Pgct", pgct);
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 我的更多申请
    /// </summary>
    protected string MyApplyByPage()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "a.id,a.fuid,a.cUser,fDptid,cFilename,fFileclid,cFilesize,fFiletype,a.tDate,a.cPic,cCk,cDescribe,fIschecked,fIsonsale,fDownloads,fApplyforupdate,fApplyforsale,cOffsale,tShsj,fShjg,cShms","[t_Filecollection] a with(nolock) join [t_Filesrecording] b with(nolock) on a.id = b.fFileid where b.cType = 1 and a.fuid = " + _uid ," order by a.id desc ", out rsct, out pgct);
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("Rsct", rsct);
        jo.Add("Pgct", pgct);
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 我的更多消息
    /// </summary>
    protected string MyMessageByPage()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if(_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "a.id,a.fuid,a.cUser,fDptid,cFilename,fFileclid,cFilesize,fFiletype,a.tDate,a.cPic,cCk,cDescribe,fIschecked,fIsonsale,fDownloads,fApplyforupdate,fApplyforsale,cOffsale,tShsj,fShjg,cShms","[t_Filecollection] a with(nolock) join [t_Filesrecording] b with(nolock) on a.id = b.fFileid where b.cType = 1 and a.fuid = " + _uid ," order by a.id desc ", out rsct, out pgct);
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Pic = dr["cPic"].ToString();
                inv.Describe = dr["cDescribe"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("Rsct", rsct);
        jo.Add("Pgct", pgct);
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 登录接口 
    /// </summary>
    protected string Login()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _user = func.safeStr(Req("user"));
        string _pass = func.safeStr(Req("pwd"));
        _pass = inc.makpass(_pass);
        DataTable dt = db.dtTable("select * from [t_Users] where cUser = '" + _user + "' and cPwd = '" + _pass + "'");
        if (dt.Rows.Count == 0)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "手机或密码不正确");
            return JsonConvert.SerializeObject(jo);
        }
        else
        {
            DataRow dr = dt.Rows[0];
            //输出Cookies        
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("zsk05", func.base64En(dr["cUser"].ToString())));
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("zsk06", _pass));
            HttpContext.Current.Response.Cookies.Add(new HttpCookie("op", new KeyPwd().Encrypto(dr["id"].ToString())));
            jo.Add("Result", 0);
            jo.Add("Message", "");
            return JsonConvert.SerializeObject(jo);
        }
    }

    /// <summary>
    /// 退出 
    /// </summary>
    protected string LoginOut()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        HttpCookie zsk05 = new HttpCookie("zsk05");
        zsk05.Expires = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Cookies.Add(zsk05);

        HttpCookie zsk06 = new HttpCookie("zsk06");
        zsk06.Expires = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Cookies.Add(zsk06);

        HttpCookie op = new HttpCookie("op");
        zsk06.Expires = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Cookies.Add(op);

        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 密码修改
    /// </summary>
    protected string EditPass()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        string _pass1 = func.safeStr(Req("pass1"));     //旧密码
        string _pass2 = func.safeStr(Req("pass2"));     //新密码
        string _pass3 = func.safeStr(Req("pass3"));     //新密码确认        
        if (string.IsNullOrEmpty(_pass1))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "旧密码不能为空");
            return JsonConvert.SerializeObject(jo);
        }
        if (string.IsNullOrEmpty(_pass2) || string.IsNullOrEmpty(_pass3))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "新密码不能为空");
            return JsonConvert.SerializeObject(jo);
        }
        if (_pass2 != _pass3)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "新密码重复密码不正确");
            return JsonConvert.SerializeObject(jo);
        }
        _pass1 = inc.makpass(_pass1);
        int ct = db.ExecInt("select count(1) from [t_Users] where id = " + _uid + " and cPwd='" + _pass1 + "'");
        if (ct == 0)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "旧密码不正确");
            return JsonConvert.SerializeObject(jo);
        }

        //至此可以开始更改密码
        _pass2 = inc.makpass(_pass2);
        db.ExecNQ("update [t_Users] set cPwd ='" + _pass2 + "' where id = " + _uid);
        jo.Add("Result", 2);
        jo.Add("Message", "旧密码不正确");
        return JsonConvert.SerializeObject(jo);
    }

    protected string getUid()
    {
        string _uid = getID(HttpUtility.UrlDecode(func.NoNull(Req("uid")).Replace("+", "%2b")));
        if (_uid == "0")
            _uid = pg.usid();
        return _uid;
    }

    /// <summary>
    /// 解密id
    /// </summary>
    protected string getID(string OpenID)
    {
        int id = 0;
        if (!string.IsNullOrEmpty(OpenID) && int.TryParse(new KeyPwd().Decrypto(OpenID), out id))
            return id.ToString();
        else
            return "0";
    }

    protected string Req(string s)
    {
        return HttpContext.Current.Request[s];
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}